﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using LitJson;
using Newtonsoft.Json.Linq;

namespace Business
{
    public class GetPicture
    {
        public static string ReadPicture(string sweb, string jylsh, string zpzl)//获取图片
        {
            var value_98 = new JObject();
            value_98.Add(new JProperty("jylsh", jylsh));
            value_98.Add(new JProperty("zpzl", zpzl));

            var data_90List = new JArray();
            var data_90 = new JObject();
            data_90.Add(new JProperty("sjbs", "9996"));
            data_90.Add(new JProperty("param", value_98));
            data_90List.Add(data_90);

            string sjieguo = Postbase64(sweb, getjson(data_90List, new JArray()), "", gethead("9996"));
            return sjieguo;
        }

        public static string Postbase64(string Url, string Data, string Referer, string s_head)
        {
            try
            {
                //SaveLog("接口数据", "head", s_head + "\r\n");
                //SaveLog("接口数据", "SendData", Data + "\r\n");

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                request.Method = "POST";
                request.Headers["head"] = s_head;

                byte[] bytes = Encoding.UTF8.GetBytes(Data);
                request.ContentType = "application/json; charset=UTF-8";
                request.ContentLength = bytes.Length;
                Stream myResponseStream = request.GetRequestStream();
                myResponseStream.Write(bytes, 0, bytes.Length);

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader myStreamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();

                if (response != null)
                {
                    response.Close();
                }
                if (request != null)
                {
                    request.Abort();
                }
                //  //string retString = "{\"fhjg\":\"1\",\"cwdm\":null,\"fhxxms\":null,\"data\":[{\"zpzl\":\"0201\",\"path\":\"\",\"zptime\":\"2024 - 03 - 08 10:58:45\",\"shujuliu\":\"00000000000000000000\"}]}";
                //JObject jsonObject = JObject.Parse(retString);
                //string fhjg = (string)jsonObject["fhjg"];
                //string data = (string)jsonObject["data"].ToString();

                //JArray jsonArray = JArray.Parse(data);
                //JObject firstObject = (JObject)jsonArray[0];
                //string  returnString = firstObject["shujuliu"].ToString();
                // retString = (string)resultData;

                //SaveLog("接口数据", "return", retString.Contains(returnString) + "\r\n");
                //return returnString;
                return retString;
            }
            catch (Exception ex)
            {
                SaveLog("接口数据", "error", ex.Message + "\r\n" + ex.StackTrace + "\r\n");
                return ex.Message;
            }
        }
        public static string getjson(JArray Data, JArray Files, string sign = "")
        {
            JObject data = new JObject();
            data.Add("data", Data);
            data.Add("files", Files);
            data.Add("sign", sign);
            return data.ToString();
        }
        public static string gethead(string wgcdbh)
        {
            JsonData head = new LitJson.JsonData();
            head["fwxlh"] = "";                                       //fwxlh	访问序列号
            head["wglb"] = "1";                                        //wglb	外挂类别
            head["jgbh"] = "";                                        //jgbh	机构编号（考场编号、检测站编号等）
            head["dxbj"] = "1";                                        //dxbj	读写标记
            head["wgcdbh"] = wgcdbh;                                      //wgcdbh	外挂菜单编号
            head["czrglbm"] = "1";                                     //czrglbm	操作人管理部门
            head["czrsfzmhm"] = "";                                   //czrsfzmhm	操作人身份证明号码（脱敏） 
            head["czrxm"] = "";                                       //czrxm	操作人姓名 使用UrlEncoder进行UTF-8编号后传值
            head["zdbh"] = "1";                                        //zdbh	终端编号
            head["zdsjip"] = "";                                    //zdsjip	终端实际ip
            head["zdsbmac"] = "";                                   //zdsbmac	终端mac地址
            head["zdsblx"] = "0";                                     //zdsblx	终端设备类型 （0：非移动端，1：移动端）
            head["zdsbszd"] = ("综检高拍");                         //zdsbszd	终端设备所在地名称 使用UrlEncoder进行UTF-8编号后传值
            head["qqsjc"] = DateTime.Now.ToString("yyyyMMddHHmmss");  //qqsjc	请求时间戳YYYYMMDDHHmmss
            return head.ToJson();
        }

        public static void SaveLog(string fileName, string jkid, string message)
        {
            StringBuilder path = new StringBuilder();
            StringBuilder pathRoot = new StringBuilder();
            try
            {
                pathRoot.Append(AppDomain.CurrentDomain.BaseDirectory);
                pathRoot.Append("\\log" + "\\");
                pathRoot.Append(DateTime.Now.ToString("yyyyMMdd"));
                if (!Directory.Exists(pathRoot.ToString()))
                {
                    Directory.CreateDirectory(pathRoot.ToString());
                }
                path.Append(pathRoot.ToString() + "\\");
                path.Append(fileName + DateTime.Now.ToString("HH"));
                path.Append(@".log");
                if (File.Exists(path.ToString()))
                {
                    StreamWriter sr = File.AppendText(path.ToString());
                    sr.WriteLine(DateTime.Now.ToString() + "【" + jkid + "】" + message);
                    sr.Close();
                }
                else
                {
                    StreamWriter sr = File.CreateText(path.ToString());
                    sr.WriteLine(DateTime.Now.ToString() + "【" + jkid + "】" + message);
                    sr.Close();
                }

            }
            catch (Exception)
            {

            }

        }
        public static Image return_image(string shujuliu)
        {
            if (shujuliu.Length > 100)
            {

                byte[] contentEX;
                contentEX = Convert.FromBase64String(shujuliu);
                //return strwgbh.Substring(19);
                Image imgex = 图片压缩.byteToImage(contentEX);
                return imgex;

            }
            return null;

        }

    }
}
